package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 根据类型查询菜品分类
     * 菜品属于分类1，套餐属于分类2
     * @param type
     * @return
     */
    @Override
    public List<Category> getCategory(Integer type) {
        List<Category> categoryList= categoryMapper.selectByCategoryType(type);
//        //分类被禁用则从集合移除
//        for (int i = 0; i < categoryList.size(); i++) {
//            if (categoryList.get(i).getStatus() == 0) {
//                categoryList.remove(i);
//                i--;
//            }
//        }
        return categoryList;
    }


    /**
     * 分类分页查询展示
     * @param dto
     * @return
     */
    @Override
    public PageResult queryByPage(CategoryPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        List<Category> categoryList = categoryMapper.selectByPage(category);
        Page page = (Page) categoryList;
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 新增分类
     * @param dto
     */

    @Override
    public void save(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.insert(category);
    }


    /**
     * 修改分类信息
     * @param dto
     */
    @Override
    public void categoryChange(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        categoryMapper.update(category);
    }

    /**
     * 修改分类状态
     * @param status
     * @param id
     */
    @Override
    public void statusChange(Integer status, Long id) {

//        List<Dish> dish = dishMapper.selectDishByCategoryId(id);
//        if (dish.size() >0 ) {
//            throw new UpdateStatusException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH_UPDATE);
//        }
//
//        Integer setmealCount= setmealMapper.selectSetmealsByCategoryId(id);
//        if (setmealCount > 0) {
//            throw new UpdateStatusException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL_UPDATE);
//        }


        Category category= Category.builder()
                .status(status)
                .id(id)
                .build();
        categoryMapper.update(category);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void delete(Long id) {
        //比较复杂 逻辑外键关联菜品表和套餐表 还要判断分类状态（延用菜品、账号、和套餐的逻辑）
        Integer status =categoryMapper.queryById(id);
        if (status == StatusConstant.ENABLE) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_ON_SALE);
        }
        Dish dishId =Dish.builder()
                .categoryId(id)
                .build();
        List<Dish> dish = dishMapper.selectDishByCategoryId(dishId);
        if (dish.size() >0 ) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        Setmeal setmeal=Setmeal.builder()
                .categoryId(id)
                .build();
        List<Setmeal> setmealList = setmealMapper.selectSetmealByCategoryId(setmeal);
        if (setmealList.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        categoryMapper.delete(id);
    }


    /**
     * 用户端条件分类查询
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        List<Category> categoryList = categoryMapper.selectByCategoryType(type);
        return categoryList;
    }


}
